<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-menu"></i> 分类列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
               <el-select v-model="searchIsShow" clearable="true" placeholder="是否显示" class="handle-select mr10">
                    <el-option key="1" label="是" value="1"></el-option>
                    <el-option key="0" label="否" value="0"></el-option>
                    <el-option key=" " label=" " value=" "></el-option>
                </el-select>
                <el-input v-model="searchName"  clearable="true" placeholder="请输入分类名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="search" @click="search">搜索</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="orderBy" label="序号" sortable width="150" align="center">
                </el-table-column>
                <el-table-column prop="name" label="分类名称" width="120"  align="center">
                </el-table-column>
                <el-table-column prop="isLeaf" label="叶子节点" width="120" :formatter="formatterIsLeaf" align="center">
                </el-table-column>
                <el-table-column prop="isShow" label="是否显示" :formatter="formatterIsShow" align="center" >
                </el-table-column>
                <el-table-column prop="parentName" label="父类名称"  width="150" align="center" :formatter="formatterParentName">
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">  
                        <!-- 插槽作用域 -->
                        <el-button type="primary"  icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger"  icon="el-icon-delete"  @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <!-- current-change 是elementui自带的方法 -->
                <el-pagination background @current-change="handleCurrentChange"
                               layout="prev, pager, next"
                               :total="total" :page-size.sync="pageSize" :current-page.sync="page">
                </el-pagination>
            </div>
        </div>





    </div>
</template>

<script>
    export default {
        name: 'CategoryTable',
        data() {
            return {
                url: './vuetable.json',
                tableData: [],
                page: 1,   //当前页
                pageSize:5,
                total:0,
                multipleSelection: [],
                select_cate: '',
                select_word: '',
                del_list: [],
                is_search: false,
                editVisible: false,
                delVisible: false,
                form: {
                    parentId: '',
                    name: '',
                    isShow: ''
                },
                idx: -1,
                searchName:'',
                searchIsShow:[]
            }
        },
        activated(){
            this.getData();
        },
        created() {
            this.getData();

        },
        computed: {
            data() {
                return this.tableData.filter((d) => {
                    let is_del = false;
                    for (let i = 0; i < this.del_list.length; i++) {
                        if (d.name === this.del_list[i].name) {
                            is_del = true;
                            break;
                        }
                    }
                    if (!is_del) {
                        if (d.address.indexOf(this.select_cate) > -1 &&
                            (d.name.indexOf(this.select_word) > -1 ||
                                d.address.indexOf(this.select_word) > -1)
                        ) {
                            return d;
                        }
                    }
                })
            }
        },
        methods: {
            // 分页导航
            handleCurrentChange(val) {
                this.page = val;
                this.search();
            },
            // 获取 easy-mock 的模拟数据
            getData() {
                let self = this;
                let url = this.HOST + "/category/all?page=" + this.page + "&size=" + this.pageSize
                this.$axios.post(url, {}).then((res) => {
                    console.log(res.data)
                    if (res.data.code === 100) {
                        self.tableData = res.data.data;
                        self.total = res.data.count;
                        self.page = res.data.page;

                    } else {
                        self.$message.error(res.data.mesg)
                    }
                })
            },
            //点击搜索按钮
            search() {
                let self = this;
                let url = this.HOST + "/category/all?page=" + this.page + "&size=" + this.pageSize
                this.$axios.post(url, {
                    name:self.searchName,
                    isShow:self.searchIsShow
                }).then((res) => {
                    if (res.data.code === 100) {
                        self.tableData = res.data.data;
                        self.total = res.data.count;
                        self.page = res.data.page;
                    } else {
                        self.$message.error(res.data.mesg)
                    }
                })

            },
            // 参数行、列
            formatterIsShow(row, column) {
               if(row.isShow==='1'){
                   return '是';
               }else{
                   return '否';
               }
            },
            formatterParentName(row, column) {
                if(row.parentName===''){
                    return '-';
                }else{
                    return row.parentName;
                }
            },
            formatterIsLeaf(row, column) {
                if(row.isLeaf==='1'){
                    return '是';
                }else{
                    return '否';
                }
            },
            filterTag(value, row) {
                return row.tag === value;
            },
            handleEdit(index, row) {
                var self=this;
                //self.$router.push('/category/edit'+row.id);
                //传递id值
                self.$router.push({ name: 'category-edit', params: { id: row.id }})
            },
            handleDelete(index, row) {
                let self = this;
                this.$confirm('删除不可恢复，确定要删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    let url = this.HOST + "/category?id="+row.id;
                    self.$axios.delete(url, {}).then((res) => {
                        // console.log(res.data)
                        if (res.data.code === 100) {
                            self.$message.success(res.data.mesg);
                            self.getData();

                        } else {
                            self.$message.error(res.data.mesg)
                        }
                    })



                }).catch(() => {});
            },
            delAll() {
                const length = this.multipleSelection.length;
                // console.log(this.multipleSelection)
                let str = '';
                this.del_list = this.del_list.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + ' ';
                //    str=str+this.multipleSelection[i].name + ' '
                }
                this.$message.error('删除了' + str);
                this.multipleSelection = [];
                console.log(this.multipleSelection)
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 保存编辑
            saveEdit() {
                this.$set(this.tableData, this.idx, this.form);
                this.editVisible = false;
                this.$message.success(`修改第 ${this.idx+1} 行成功`);
            },
            // 确定删除
            deleteRow(){
                this.tableData.splice(this.idx, 1);
                this.$message.success('删除成功');
                this.delVisible = false;
            }
        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
    .table{
        width: 100%;
        font-size: 14px;
    }
    .red{
        color: #ff0000;
    }
    .mr10{
        margin-right: 10px;
    }
   
</style>
<!--查：1、根据接口拿到res中的data 赋值到data 中 tabledata。再循环。通过network查看每个名称，写上。-->
<!--2、在created钩子函数中，调用此方法，当页面初次加载就渲染页面-->
<!--分页 调用element-ui中的参数。再data中分别设置当前页、总页数、每页显示的数据。进行绑定-->
<!--在调用查询接口的时候，将res中的total、pageSize赋值到data中（必须与res中的一一对应）-->
<!--在跳转到下一页时，再次调用getdata（）方法，拿到对应数据-->

<!--删-->
<!--1、在element-ui中找到组件模板-->
<!--通过id拿到该条数据进行删除.在刷新页面-->

